﻿using System;
using System.Collections.Generic;
using System.Text;
using HR.Appointments.DataLayer;

namespace HR.Appointments.Service
{
    /// <summary>
    /// Entity class for activites
    /// </summary>
    public class ActivityEntity
    {
        #region Member variables
        private int _companyId;
        private int _activityId;
        private string _name;
        private int _duration;
        #endregion

        #region Constructors
        private ActivityEntity(int activityId, int companyId, string name, int duration)
        {
            _companyId = companyId;
            _activityId = activityId;
            _name = name;
            _duration = duration;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Activity identifier
        /// </summary>
        public int ActivityId
        {
            get
            {
                return _activityId;
            }
            set
            {
                _activityId = value;
            }
        }

        /// <summary>
        /// Activity name.
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        /// <summary>
        /// Company identifier
        /// </summary>
        public int CompanyID
        {
            get
            {
                return _companyId;
            }
            set
            {
                _companyId = value;
            }
        }

        /// <summary>
        /// Duration of the activity in minutes
        /// </summary>
        public int Duration
        {
            get
            {
                return _duration;
            }
            set
            {
                _duration = value;
            }
        }
        #endregion

        #region Internal Methods
        /// <summary>
        /// Convert this object to datalayer 
        /// </summary>
        /// <param name="data">Datalayer entity</param>
        /// <returns>A datalayer entity with this objects values</returns>
        internal Activity ToData(Activity data)
        {
            if (data == null)
            {
                data = new Activity();
            }
            data.CompanyId = _companyId;
            data.Name = _name;
            data.Duration = _duration;
            return data;
        }
        #endregion

        #region Override Methods
        /// <summary>
        /// Determines whether the specified Object is equal to the current Object.
        /// </summary>
        /// <param name="obj">The Object to compare with the current Object.</param>
        /// <returns>true if the specified Object is equal to the current Object; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType()) return false;
            ActivityEntity eql = (ActivityEntity)obj;
            return (eql.ActivityId == ActivityId);
        }

        /// <summary>
        /// Serves as a hash function for a this type.
        /// </summary>
        /// <returns>A hash code for the current Object.</returns>
        public override int GetHashCode()
        {
            return _activityId.GetHashCode(); // ^ _companyId.CompanyId.GetHashCode();
        }

        /// <summary>
        /// Returns a String that represents the current Object.
        /// </summary>
        /// <returns>A String that represents the current Object.</returns>
        public override string ToString()
        {
            return _activityId + " " + _name;
        }
        #endregion

        #region Static Methods
        /// <summary>
        /// Loads a datalayer entity into a service entity.
        /// </summary>
        /// <param name="row"></param>
        /// <returns>A loaded entity</returns>
        internal static ActivityEntity Load(HR.Appointments.DataLayer.Activity row)
        {
            if (row == null) return null;
            return new ActivityEntity(row.ActivityId, row.CompanyId, row.Name, row.Duration);;
        }
        
        /// <summary>
        /// Create a new entity
        /// </summary>
        /// <param name="companyID">Company identifier</param>
        /// <param name="name">Name of the activity</param>
        /// <param name="duration">Duration of the activity in minutes</param>
        /// <returns></returns>
        public static ActivityEntity Create(int companyID, string name, int duration)
        {
            return new ActivityEntity(0, companyID, name, duration);;
        }
        #endregion  
    }
}
